//
//  MBCommodityDetailCell.swift
//  iPhone
//
//  Created by Toj on 8/9/22.
//
//  商品规格 -> 独立内容包

import UIKit

protocol MBCommodityDetailCellDelegate: NSObjectProtocol {
    func commodityDetailCellDidTapBuy(_ view: MBCommodityDetailCell)
}

private let buyButtonHeight: CGFloat = 32

// MARK: - 懒加载(属性), 变量, 初始化init
class MBCommodityDetailCell: UICollectionViewCell {
    
    weak var delegate: MBCommodityDetailCellDelegate?
    
    var selectedIndexPath: IndexPath?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupMakeAddSubviews()
        setupMakeInitialize()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupMakeLayoutSubviews()
    }
    
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 8
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize(width: 104, height: 124)
        
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.bounces = false
        view.scrollsToTop = true
        view.alwaysBounceVertical = true
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.backgroundColor = .white
        view.register(MBCommodityDetailSpecCell.cell_class, forCellWithReuseIdentifier: MBCommodityDetailSpecCell.cell_identifier)
        view.dataSource = self
        view.delegate = self
        return view
    }()
    
    private lazy var tipsView = MBCommodityDetailTipsView()
    
    private lazy var leftArrowImageView: UIImageView = {
        let view = UIImageView()
        view.image = "com_commodity_arrow_left".to_image
        return view
    }()
    private lazy var leftShadowImageView: UIImageView = {
        let view = UIImageView()
        view.isHidden = true
        view.image = "com_commodity_shadow_left".to_image
        return view
    }()
    private lazy var rightArrowImageView: UIImageView = {
        let view = UIImageView()
        view.image = "com_commodity_arrow_right".to_image
        return view
    }()
    private lazy var rightShadowImageView: UIImageView = {
        let view = UIImageView()
        // view.isHidden = true
        view.image = "com_commodity_shadow_right".to_image
        return view
    }()
    
    private lazy var buyButton: UIButton = {
        let view = UIButton()
        view.cm_cornerRadius = buyButtonHeight * 0.5
        view.setTitle("BUY NOW", for: .normal)
        view.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14)
        view.setTitleColor(UIColor(hex:"#775E33"), for: .normal)
        view.addTarget(self, action: #selector(didTapBuy), for: .touchUpInside)
        return view
    }()
    
    private lazy var agreeCheckBox: UIButton = {
        let view = UIButton()
        view.isUserInteractionEnabled = false
        view.setTitle("I agree with the", for: .normal)
        view.setTitleColor(UIColor(hex: "#666666"), for: .normal)
        view.setImage("commodity_agreeCheckBox_sel".to_image, for: .normal)
        view.setContentHuggingPriority(.dragThatCanResizeScene, for: .horizontal)
        view.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        view.sizeToFit()
        return view
    }()
    
    private lazy var serviceLabel: UILabel = {
        let view = UILabel()
        
        let font = UIFont.systemFont(ofSize: 14)
        let titleColor = UIColor(hex: "#0087EB")
        
        let attributed = "Terms of Service".to_attributedString(font: font, color: titleColor, lineStyle: .strikethrough)
        view.attributedText = attributed
        view.font = font
        view.textColor = titleColor
        
        return view
    }()
}

// MARK: - 点击事件
extension MBCommodityDetailCell {
    
    @objc private func didTapBuy() {
        delegate?.commodityDetailCellDidTapBuy(self)
    }
}

// MARK: - Private
extension MBCommodityDetailCell {
    
}

// MARK: - Private
extension MBCommodityDetailCell {
    
}

// MARK: - 代理事件 UICollectionViewDataSource
extension MBCommodityDetailCell: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MBCommodityDetailSpecCell.cell_identifier, for: indexPath, convert: MBCommodityDetailSpecCell.self)
        if selectedIndexPath == nil {
            selectedIndexPath = indexPath
        }
        cell.isSelected_ = selectedIndexPath == indexPath
        cell.model = nil
        return cell
    }
}

// MARK: - 代理事件 UICollectionViewDataSource
extension MBCommodityDetailCell: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectedIndexPath = indexPath
        collectionView.reloadData()
        collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offsetX = scrollView.contentOffset.x
        print("-->toj: offsetY:\(offsetX)")
        if offsetX < 10 {
            if leftShadowImageView.isHidden { return }
            leftShadowImageView.isHidden = true
            return
        }
        
        if leftShadowImageView.isHidden {
            leftShadowImageView.isHidden = false
        }
       
        if offsetX > (scrollView.contentSize.width - CGFloat.sr_width - 16 * 2 - 10) {
            if rightShadowImageView.isHidden { return }
            rightShadowImageView.isHidden = true
            return
        }
        
        if rightShadowImageView.isHidden {
            rightShadowImageView.isHidden = false
        }
    }
}

// MARK: - 初始化
extension MBCommodityDetailCell {
    
    private func setupMakeAddSubviews() {
        
        contentView.addSubview(collectionView)
        contentView.addSubview(tipsView)
        contentView.addSubview(buyButton)
        contentView.addSubview(agreeCheckBox)
        contentView.addSubview(serviceLabel)
        
        contentView.addSubview(leftShadowImageView)
        leftShadowImageView.addSubview(leftArrowImageView)
        contentView.addSubview(rightShadowImageView)
        rightShadowImageView.addSubview(rightArrowImageView)
           
    }
    
    private func setupMakeLayoutSubviews() {
        
        collectionView.snp.makeConstraints { make in
            make.left.top.right.equalTo(contentView)
            make.height.equalTo(124)
        }
        
        buyButton.snp.makeConstraints { make in
            make.bottom.equalTo(contentView.snp.bottom).offset(-48)
            make.left.equalTo(16)
            make.right.equalTo(contentView.snp.right).offset(-16)
            make.height.equalTo(buyButtonHeight)
        }
        
        let width = agreeCheckBox.frame.width
        agreeCheckBox.snp.makeConstraints { make in
            make.left.equalTo(buyButton)
            make.height.equalTo(48)
            make.bottom.equalTo(contentView.snp.bottom)
            make.width.equalTo(width)
        }
        
        serviceLabel.snp.makeConstraints { make in
            make.left.equalTo(agreeCheckBox.snp.right).offset(2)
            make.centerY.equalTo(agreeCheckBox)
            make.right.equalTo(buyButton)
        }
        
        tipsView.snp.makeConstraints { make in
            make.top.equalTo(collectionView.snp.bottom).offset(8)
            make.left.right.equalTo(buyButton)
            make.bottom.equalTo(buyButton.snp.top).offset(-10)
        }
        
        leftShadowImageView.snp.makeConstraints { make in
            make.top.bottom.left.equalTo(collectionView)
            make.width.equalTo(40)
        }
        
        leftArrowImageView.snp.makeConstraints { make in
            make.left.equalTo(3)
            make.centerY.equalTo(leftShadowImageView)
            make.width.equalTo(24)
            make.height.equalTo(24)
        }
        
        rightShadowImageView.snp.makeConstraints { make in
            make.top.bottom.right.equalTo(collectionView)
            make.width.equalTo(leftShadowImageView)
        }
        
        rightArrowImageView.snp.makeConstraints { make in
            make.centerY.width.height.equalTo(leftArrowImageView)
            make.right.equalTo(rightShadowImageView).inset(3)
        }
        
        layoutIfNeeded()
        let colors = ["#F5DB9D".to_color.cgColor, "#E8CD8C".to_color.cgColor]
        buyButton.gd_add(colors: colors, direction: .leftToRight)
    }
    
    private func setupMakeInitialize() {
        
        buyButton.backgroundColor = UIColor(hex: "#F5DB9D")
        
        collectionView.contentInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
        
    }
}
